<template>
  <div>
    <Row>
      <Input v-model="listQuery.name" placeholder="资源名称" @on-enter="handleFilter" style="width: 200px;"></Input>
      <Button type="primary" icon="ios-search" @click="handleFilter">搜索</Button>
      <Button type="primary" icon="ios-search" v-if="menuManager_btn_element_add" style="margin-left: 10px;" @click="handleCreate">添加</Button>
    </Row>
    <Tables ref="table" searchable search-place="top" :loading="listLoading" :columns="columns" v-model="list">
      <template slot="action" slot-scope="scope">
        <Button v-if="menuManager_btn_element_edit" type="success" size="small" @click="handleUpdate(scope.row)">编辑</Button>
        <Button v-if="menuManager_btn_element_del" type="warning" size="small" @click="handleDelete(scope.row)">删除</Button>
      </template>
    </Tables>
    <Modal :title="textMap[dialogStatus]" v-model="dialogFormVisible" :mask-closable='false' :width="500">
      <Form ref="form" :model="form" :label-width="100" :rules="rules">
        <FormItem label="资源编码" prop="code">
          <Input v-model="form.code" placeholder="请输入资源编码"/>
        </FormItem>
        <FormItem label="资源类型" prop="type">
          <Select v-model="form.type" placeholder="请输入资源类型">
            <Option v-for="item in  typeOptions" :key="item.id" :label="item.labelDefault" :value="item.value"> </Option>
          </Select>
        </FormItem>
        <FormItem label="资源名称" prop="name">
          <Input v-model="form.name" placeholder="请输入资源名称"/>
        </FormItem>
        <FormItem label="资源名称" prop="uri">
          <Input v-model="form.uri" placeholder="请输入资源地址"/>
        </FormItem>
        <FormItem label="资源请求类型" prop="method">
          <Select v-model="form.method" placeholder="请输入资源请求类型">
            <Option v-for="item in  methodOptions" :key="item" :label="item" :value="item"> </Option>
          </Select>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" @click="cancel('form')">取消</Button>
        <Button type="primary" v-if="dialogStatus=='create'" @click="create('form')">确 定</Button>
        <Button type="primary" v-else @click="update('form')">确 定</Button>
      </div>
    </Modal>
  </div>
</template>
<script>
import {
  page,
  addObj,
  getObj,
  delObj,
  putObj
} from '@/api/admin/element/index'
import { mapGetters } from 'vuex'
import { getTypeValue } from '@/api/dict/dictValue/index'
import Tables from '_c/tables'
export default {
  name: 'menu-element',
  components: {
    Tables
  },
  data () {
    return {
      methodOptions: ['GET', 'POST', 'PUT', 'DELETE'],
      typeOptions: ['uri', 'button'],
      form: {
        code: undefined,
        type: undefined,
        name: undefined,
        uri: undefined,
        menuId: undefined,
        method: undefined,
        description: undefined
      },
      columns: [
        { title: '操作',
          key: 'action',
          align: 'center',
          render: (h, params) => {
            return h(
              'div',
              this.$refs.table.$scopedSlots.action(params)
            )
          }
        },
        { title: '资源编码', key: 'code' },
        { title: '资源类型', key: 'type' },
        { title: '资源名称', key: 'name' },
        { title: '资源地址', key: 'uri' },
        { title: '资源请求类型', key: 'method' },
        { title: '描述', key: 'description' }

      ],
      rules: {
        code: [{
          required: true,
          message: '请输入资源编码',
          trigger: 'blur'
        }
        ],
        type: [{
          required: true,
          message: '请输入资源类型',
          trigger: 'blur'
        },
        {
          min: 2,
          max: 20,
          message: '长度在 2 到 20 个字符',
          trigger: 'blur'
        }
        ],
        name: [{
          required: true,
          message: '请输入资源名称',
          trigger: 'blur'
        },
        {
          min: 2,
          max: 20,
          message: '长度在 2 到 20 个字符',
          trigger: 'blur'
        }
        ],
        uri: [{
          required: true,
          message: '请输入',
          trigger: 'blur'
        },
        {
          min: 3,
          max: 40,
          message: '长度在 3 到 40 个字符',
          trigger: 'blur'
        }
        ],
        method: [{
          required: true,
          message: '请输入资源请求类型',
          trigger: 'blur'
        },
        {
          min: 3,
          max: 20,
          message: '长度在 3 到 20 个字符',
          trigger: 'blur'
        }
        ],
        description: [{
          required: true,
          message: '请输入',
          trigger: 'blur'
        },
        {
          min: 3,
          max: 20,
          message: '长度在 3 到 20 个字符',
          trigger: 'blur'
        }
        ]
      },
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        name: undefined,
        menuId: this.menuId
      },
      dialogFormVisible: false,
      dialogStatus: '',
      menuManager_btn_element_add: false,
      menuManager_btn_element_edit: false,
      menuManager_btn_element_del: false,
      menuId: -1,
      textMap: {
        update: '编辑',
        create: '创建'
      },
      tableKey: 0
    }
  },
  created () {
    this.getList()
    this.menuManager_btn_element_add = this.elements['menuManager:btn_element_add']
    this.menuManager_btn_element_del = this.elements['menuManager:btn_element_del']
    this.menuManager_btn_element_edit = this.elements['menuManager:btn_element_edit']
    getTypeValue('authority_element').then(response => {
      this.typeOptions = response.data.rows
    })
  },
  computed: {
    ...mapGetters([
      'elements'
    ])
  },
  methods: {
    getList () {
      this.listLoading = true
      this.listQuery.menuId = this.menuId
      page(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter () {
      this.getList()
    },
    handleSizeChange (val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.listQuery.page = val
      this.getList()
    },
    handleCreate () {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleUpdate (row) {
      getObj(row.id).then(response => {
        this.form = response.data
        this.dialogFormVisible = true
        this.dialogStatus = 'update'
      })
    },
    handleDelete (row) {
      this.$Modal.confirm({
        title: '提示',
        content: '此操作将永久删除, 是否继续?',
        onOk: () => {
          delObj(row.id).then(() => {
            this.$Notice.success({
              title: '成功',
              message: '删除成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
        }
      })
    },
    create (formName) {
      const set = this.$refs
      this.form.menuId = this.menuId
      set[formName].validate(valid => {
        if (valid) {
          addObj(this.form).then(() => {
            this.dialogFormVisible = false
            this.getList()
            this.$Notice.success({
              title: '成功',
              message: '创建成功'
            })
          })
        } else {
          return false
        }
      })
    },
    cancel (formName) {
      this.dialogFormVisible = false
      const set = this.$refs
      set[formName].resetFields()
    },
    update (formName) {
      const set = this.$refs
      this.form.menuId = this.menuId
      set[formName].validate(valid => {
        if (valid) {
          this.dialogFormVisible = false
          this.form.password = undefined
          putObj(this.form.id, this.form).then(() => {
            this.dialogFormVisible = false
            this.getList()
            this.$Notice.success({
              title: '成功',
              message: '创建成功'
            })
          })
        } else {
          return false
        }
      })
    },
    resetTemp () {
      this.form = {
        code: undefined,
        type: undefined,
        name: undefined,
        uri: undefined,
        menuId: undefined,
        method: undefined,
        description: undefined
      }
    }
  }
}
</script>
